package com.neuedu.funlifebbs.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.neuedu.funlifebbs.bean.Categorys;
import com.neuedu.funlifebbs.bean.Comments;
import com.neuedu.funlifebbs.bean.Labels;
import com.neuedu.funlifebbs.bean.Topics;
import com.neuedu.funlifebbs.bean.Users;
import com.neuedu.funlifebbs.service.CategorysService;
import com.neuedu.funlifebbs.service.CommentsService;
import com.neuedu.funlifebbs.service.LabelsService;
import com.neuedu.funlifebbs.service.TopicsServic;
import com.neuedu.funlifebbs.service.UsersService;

/**
 * @author jds
 *
 * @version 2019年8月22日 下午2:14:54
 */

@Controller
@RequestMapping("fronts")
public class PostController {

	@Autowired
	LabelsService labelsService;

	@Autowired
	TopicsServic topicsServic;

	@Autowired
	CategorysService categorysService;

	@Autowired
	CommentsService commentsService;

	@Autowired
	UsersService usersService;

	@RequestMapping(value = "/post", method = RequestMethod.GET)
	public String post(Model model) {
		// 查所有标签
		List<Labels> list = labelsService.findAll();
		// 查所有版块
		List<Categorys> cList = categorysService.findAll();
		model.addAttribute("list", list);
		model.addAttribute("cList", cList);
		return "front/post";
	}

	@RequestMapping(value = "/post", method = RequestMethod.POST)
	public String savePost(Topics topics) {
		Date date = new Date();
		topics.setTopictime(date);
		topicsServic.add(topics);
		int id = topics.getCategorys().getId();
		Categorys categorys = categorysService.findById(id);
		List<Topics> listTopics = topicsServic.findByCategorysId(id);

		int count = listTopics.size();
		categorys.setTopiccount(count);
		categorysService.update(categorys);
		return "redirect:/";
	}

	@RequestMapping(value = "/communitypost/{id}", method = RequestMethod.GET)
	public String community(Model model, @PathVariable int id) {
		// 根据帖子id查帖子内容
		List<Comments> listComments = commentsService.findByTopicId(id);

		model.addAttribute("floor", listComments.size() + 1);
		model.addAttribute("comments", listComments);
				
		Topics topics = topicsServic.findById(id);
		model.addAttribute("topics", topics);
		int labelid = topics.getLabel().getId();
		Labels labels = labelsService.findById(labelid);
		
		model.addAttribute("topicslabels", labels);
		
		int userid = topics.getUser().getId();
		Users users = usersService.findById(userid);
		model.addAttribute("author", users);
		return "front/communitypost";
	}

	@RequestMapping(value = "/addcomment/{id}", method = RequestMethod.POST)
	public String saveComments(Comments comments, @PathVariable int id) {
			Date date = new Date();
			comments.setCommenttime(date);
			commentsService.add(comments);

			Topics topics = topicsServic.findById(id);
			List<Comments> listComments = commentsService.findByTopicId(id);
			int count = listComments.size();

			topics.setCommentcount(count);
			topicsServic.update(topics);
			return "redirect:/";		
	}
}
